using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using SCSEngine.ResourceManagement;
using SCSEngine.Sprite;
using SCSEngine.Services;
using SCSEngine.Sprite.Implements;
using SCSEngine.ScreenManagement;
using TriUan.SCSEngine.Utils;
using System.Xml.Linq;
using SVLvsXNA;
using SVLvsXNA.TriUanGame;

namespace TriUan.TriUanGame.GameScreen.Modes
{
    public class ClassicMode : IMode
    {
        private static ClassicMode inst;
        private IResourceManager resourceManager;

        private ClassicMode(GamePage gamePage)
        {
            resourceManager = (IResourceManager)gamePage.Services.GetService(typeof(IResourceManager));
        }

        public static ClassicMode getInst(GamePage gamePage)
        {
            if (inst == null)
            {
                inst = new ClassicMode(gamePage);
            }
            return inst;
        }

        public void Change(GameModeScreen gameMode, IGameScreenManager manager)
        {
            gameMode.Background = resourceManager.GetResource<Texture2D>("GameMode_2");
            gameMode.icon = resourceManager.GetResource<ISprite>("ScareCrow_Classic");
            gameMode.iconPosition = new Vector2(60f, 295f);
            gameMode.iconScale = 0.5f;

            gameMode.ButtonLeft.Background = resourceManager.GetResource<ISprite>(Language.Prep + "Random");
            gameMode.ButtonLeft.Size = new Vector2(132f, 26f);
            gameMode.ButtonLeft.Visible = true;
            gameMode.ButtonLeft.Enabled = true;
            gameMode.ButtonRight.Background = resourceManager.GetResource<ISprite>(Language.Prep + "Pick");
            gameMode.ButtonLeft.Size = new Vector2(80f, 26f);
            gameMode.ButtonRight.Visible = true;
            gameMode.ButtonRight.Enabled = true;

            gameMode.Classic.Visible = false;
            gameMode.Classic.Enabled = false;

            gameMode.Creative.Visible = false;
            gameMode.Creative.Enabled = false;

            gameMode.FreeStyle.Visible = false;
            gameMode.FreeStyle.Enabled = false;

            List<string> listPattern = new List<string>();

            XDocument mXDocument = new XDocument() ;
            mXDocument = XDocument.Load("patternXml\\pattern.xml");

            var patterns = mXDocument.Descendants("Patterns");
            foreach (var p in patterns.Elements("Pattern"))
            {
                listPattern.Add(p.Value.ToString());
            }

            //
            foreach (var item in listPattern)
            {
                ShapeItem shape = new ShapeItem(manager, (SCSServices)gameMode.GamePage.Services.GetService(typeof(SCSServices)),
                (Sprite)resourceManager.GetResource<ISprite>("Item"), resourceManager.GetResource<Texture2D>("Pattern/" + item));
                shape.PatternName = item;
                gameMode.ListView.Add(shape);
                if (gameMode.ListView.LineSpace.Equals(Vector2.Zero))
                {
                    gameMode.ListView.LineSpace = new Vector2(shape.Item.Width/2, 0);
                }
            }

            
        }
    }
}
